package Action;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.jspsmart.upload.SmartUpload;

import Dao.noticedao;
import Dao.scheduledao;
import Dao.userdao;
import Entity.Notice;
import Entity.Room_ced;
import Entity.Selection;
import Entity.User;
import mapper.Noticemapper;
import mapper.Schedulemapper;

/**
 * Servlet implementation class addUser
 */
@WebServlet("/addUser")
public class addUser extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
    // �ϴ��ļ��洢Ŀ¼
    private static final String UPLOAD_DIRECTORY = "upload";
 
    // �ϴ�����
    private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 3;  // 3MB
    private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
    private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; // 50MB
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("gbk"); 
		response.setCharacterEncoding("gbk");
		response.setContentType("text/html; charset=gbk"); 
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		
		DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(MEMORY_THRESHOLD);
        factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
 
        ServletFileUpload upload = new ServletFileUpload(factory);

        upload.setFileSizeMax(MAX_FILE_SIZE);

        upload.setSizeMax(MAX_REQUEST_SIZE);

        upload.setHeaderEncoding("GBK"); 

        String uploadPath = getServletContext().getRealPath("/") + File.separator + UPLOAD_DIRECTORY;

        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
 
        SmartUpload smartUpload = new SmartUpload();
		ServletConfig config = this.getServletConfig();
		smartUpload.initialize(config, request, response);
		
	 try {
		 	smartUpload.upload();
		 	com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0);
			String name = smartFile.getFileName();
			String str=session.getServletContext().getRealPath("/");
			smartFile.saveAs(str+"/upload/"+name, SmartUpload.SAVE_AUTO);
			request.setAttribute("message",
                    "�γ̴����ɹ�!");
			String filePath=str+"/upload/"+name;
			System.out.println(filePath); 
			File file = new File(filePath);
            InputStreamReader read = new InputStreamReader(
            new FileInputStream(file),"utf-8");
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            userdao usd=new userdao();
            while((lineTxt = bufferedReader.readLine()) != null){
					String [] spString = lineTxt.split("\\s+");
					int i=0;
					String sid = null;
					User ug=new User();
					for(String ss : spString){
						if(i==0){
							ug.setUid(ss);
						}
						else if(i==1){
							ug.setUname(ss);
						}
						else if(i==2){
							ug.setPwd(ss);
						}
						else{
							ug.setUtype(Integer.parseInt(ss));
						}	
						
						i++;
					}
					ug.setTel("未输入");
					System.out.println(ug);
					usd.CreatAccount(ug);
			}
            read.close();
            
            
        } catch (Exception ex) {
            request.setAttribute("message",
                    "������Ϣ: " + ex.getMessage());
        }
	 
	 request.getRequestDispatcher("queryCourses").forward(request,response);
	}
		
		
	}


